package me.zhengjie.modules.customer_app.rest;

import cn.hutool.json.JSONObject;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.customer.domain.TCustomerUser;
import me.zhengjie.modules.customer.service.TCustomerInfoService;
import me.zhengjie.modules.customer.service.TCustomerUserService;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoDTO;
import me.zhengjie.modules.customer.service.dto.TCustomerUserDTO;
import me.zhengjie.modules.customer.service.mapper.TCustomerUserMapper;
import me.zhengjie.modules.customer_app.util.ValidateUtils;
import me.zhengjie.modules.equipment.service.TEquipmentService;
import me.zhengjie.modules.equipment.service.dto.TEquipmentQueryCriteria;
import me.zhengjie.modules.system.common.EnumAccidentSource;
import me.zhengjie.modules.workorder.domain.TAccident;
import me.zhengjie.modules.workorder.service.TAccidentService;
import me.zhengjie.modules.workorder.service.TWorkOrderService;
import me.zhengjie.modules.workorder.service.dto.TAccidentDTO;
import me.zhengjie.modules.workorder.service.dto.TAccidentQueryCriteria;
import me.zhengjie.modules.workorder.service.dto.TWorkOrderDTO;
import me.zhengjie.utils.HttpResponseUtils;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by bruce on 19/9/27.
 */
@RestController
@RequestMapping("/app/customer")
public class TAppCustomerUserController {

    @Autowired
    private TAccidentService tAccidentService;

    @Autowired
    private TCustomerInfoService tCustomerInfoService;

    @Autowired
    private TWorkOrderService tWorkOrderService;

    @Autowired
    private TCustomerUserService tCustomerUserService;

    @Autowired
    private TCustomerUserMapper tCustomerUserMapper;

    @Autowired
    private TEquipmentService tEquipmentService;

    @Log("customer_app获取故障信息")
    @GetMapping(value = "/accidents")
    public ResponseEntity getAccidents(@Nullable TAccidentQueryCriteria criteria,
                                       @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable){
        return new ResponseEntity(tAccidentService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @Log("customer_app增加故障信息")
    @PostMapping(value = "/addAccident")
    public ResponseEntity addAccident(@Validated @RequestBody TAccident accident) {
        TCustomerInfoDTO customerInfo = tCustomerInfoService.findById(SecurityUtils.getOwnerId());
        accident.setCustomerId(customerInfo.getId());
        accident.setCustomerName(customerInfo.getCustomerName());

        accident.setInputOperator(SecurityUtils.getName());  //这里为填报人
        accident.setInputTime(new Date());
        accident.setSource(EnumAccidentSource.CUSOTMER.getValue());
        return new ResponseEntity(tAccidentService.create(accident), HttpStatus.OK);
    }

    @Log("customer_app修改故障信息")
    @PostMapping(value = "/updateAccident")
    public ResponseEntity updateAccident(@Validated @RequestBody TAccident accident) {
        TCustomerInfoDTO customerInfo = tCustomerInfoService.findById(SecurityUtils.getOwnerId());
        accident.setCustomerId(customerInfo.getId());
        accident.setCustomerName(customerInfo.getCustomerName());

        TAccidentDTO accidentDTO = tAccidentService.findById(accident.getId());

        /*TODO@@@@@
        if(ValidateUtils.accidentEditable(accidentDTO.getOrder().getStatus())) {
            return new ResponseEntity(ValidateUtils.ACCIDENT_STATUS_UNEDITABLE,HttpStatus.BAD_REQUEST);
        }*/

        accident.setInputOperator(SecurityUtils.getName());  //这里为填报人
        accident.setInputTime(new Date());
        accident.setSource(EnumAccidentSource.CUSOTMER.getValue());
        tAccidentService.update(accident);
        return new ResponseEntity( HttpStatus.OK);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Log("customer_app查询设备信息")
    @GetMapping(value = "/equipmentinfo")
    public ResponseEntity searchCustomerEqtInfo(
            @RequestParam(value = "code",required = false)String code,
            @RequestParam(value = "name",required = false)String name ) {
        //TODO 增加设备编码查询
        if(!StringUtils.isEmpty(code)) {
            return new ResponseEntity(HttpStatus.OK);
        } else {
            Integer customerId = SecurityUtils.getOwnerId().intValue();
            TEquipmentQueryCriteria criteria = new TEquipmentQueryCriteria();
            criteria.setCustomerId(customerId);
            return new ResponseEntity(tEquipmentService.queryAll(criteria), HttpStatus.OK);
        }
    }

    @Log("customer_app修改个人信息")
    @PostMapping(value = "/updateProfile")
    public ResponseEntity updateProfile(@Validated @RequestBody TCustomerUser user) {
        TCustomerUserDTO customerUser = tCustomerUserService.findById(SecurityUtils.getUserId());
        TCustomerUser userUpdate = tCustomerUserMapper.toEntity(customerUser);

        userUpdate.setUsername(user.getUsername());
        userUpdate.setCellphone(user.getCellphone());

        tCustomerUserService.update(userUpdate);
        return new ResponseEntity("OK", HttpStatus.OK);
    }

    @Log("customer_app修改个人信息")
    @PostMapping(value = "/updatePassword")
    public ResponseEntity updatePassword(@Validated @RequestBody JSONObject user) {
        TCustomerUserDTO customerUser = tCustomerUserService.findById(SecurityUtils.getUserId());

        String originPass = user.getStr("password","");
        if(! originPass.equals(customerUser.getPassword())) {
            return HttpResponseUtils.passDefaultResponse("原有密码错误!", HttpStatus.BAD_REQUEST);
        }

        if(StringUtils.isEmpty(user.getStr("newPassword"))) {
            return HttpResponseUtils.passDefaultResponse("密码不能为空!", HttpStatus.BAD_REQUEST);
        }

        TCustomerUser userUpdate = tCustomerUserMapper.toEntity(customerUser);
        userUpdate.setPassword(user.getStr("newPassword",""));
        tCustomerUserService.update(userUpdate);
        return HttpResponseUtils.passDefaultResponse("OK",HttpStatus.OK);
    }
}
